<?php
    include '../lib/confs/Conf.php';    
    
    $updateCompensation = "UPDATE hs_hr_employee_leave_quota SET no_of_days_allotted = leave_taken WHERE leave_type_id = 'LTY007' AND emp_status != 'EST001' AND updated_at + INTERVAL 2 MONTH < NOW()";       
    query($updateCompensation,"Updated Compensation leave quotas successfull!");
    
    $updateAnnual = "UPDATE hs_hr_employee_leave_quota SET no_of_days_allotted = 
                    (leave_taken + leave_brought_forward) WHERE leave_type_id = 'LTY001' 
                    AND emp_status != 'EST001' AND employee_id 
                    IN (
                        SELECT 
                            emp_number
                        FROM
                            hs_hr_employee
                        where
                            emp_status != 'EST000' 
                            AND (   (joined_date >= '2013-01-01' AND 
                                        (   joined_date = DATE_SUB(CURDATE(), INTERVAL 3 YEAR) OR 
                                            joined_date = DATE_SUB(CURDATE(), INTERVAL 6 YEAR)
                                         )
                                         )OR 
                                     (joined_date < '2013-01-01' AND 
                                         (   '2013-01-01' = DATE_SUB(CURDATE(), INTERVAL 3 YEAR) OR 
                                             '2013-01-01' = DATE_SUB(CURDATE(), INTERVAL 6 YEAR)
                                          )
                                      )
                                 );
                    ) ";       
    query($updateAnnual,"Updated Annual leave quotas successfull!");
    
    //$updateTimeShift = "UPDATE hs_hr_employee_leave_quota SET no_of_days_allotted = 0 WHERE leave_type_id = 'LTY009' AND updated_at + INTERVAL 2 WEEK < NOW()";       
    //query($updateTimeShift);
    
    function query($query,$message='') {
        $conf = new Conf();    
        $user=$conf->dbuser;
        $password=$conf->dbpass;
        $database=$conf->dbname;
        $server=$conf->dbhost;
	$con = mysql_connect($server, $user, $password);
        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }
        @mysql_select_db($database) or die("Unable to select database " . $database . ".");      
        mysql_query($query) or die(mysql_error());        
        mysql_close();
        return $message;
    }

?>
